package com.gqzmy.springboot3.filters;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.io.IOException;
import java.util.UUID;

/**
 * @author 周小哥  gqzmy@163.com 2022/12/1 18:30
 */
public class TraceIdFilter implements Filter {
    public static final Logger logger = LoggerFactory.getLogger(TraceIdFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        logger.info("请求路径:{}", ((HttpServletRequest) request).getRequestURI());
        String uuid = UUID.randomUUID().toString();
        logger.info("生成UUID:{}", uuid);
        MDC.put("traceId", uuid);
        chain.doFilter(request, response);
    }
}
